package cn.com.cennavi.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.Logger;

import cn.com.cennavi.bean.DiDiOrignalBean;
import cn.com.cennavi.util.MapUtil;

/**
 * 生成一天didi数据，一分钟一个文件
 * 
 * @ClassName: DiDiDataEveryMinuteMake
 * @Description: TODO（这里用一句话描述这个类的作用）
 * @author bull
 * @date 2016年5月18日 下午3:50:06
 *
 */
public class DiDiDataEveryMinuteMake {
	private static Logger log = Logger.getLogger(DiDiDataEveryMinuteMake.class);
	private static SimpleDateFormat hf = new SimpleDateFormat("yyyyMMddHHmm");
	private final static String BEIJING_MESHID = "595673,595663,595662,595673,595663,595672,605602,605614,605604,605613,605603,605605,595653,595665,595675,595674,595664,595673,595672,595654,595663,595660,595653,595652,595651,595650,595662,595661,595663,595670,595671,595662,595661,595672,95671,605611,605610,605600,595662,605602,605601,595661,595673,605612,605603,595672,595670,595671,595660,595577,605600,595567,605514,595564,605505,595573,595565,605601,605504,595574,605503,605516,595575,605515,595576,605506,605507,595554,595661,605517,595660,595566,595567,595564,595565,595557,595556,595555,595554,595536,595630,595631,595553,595535,595534,595533,595652,595651,595537,595650,595576,595621,595545,595544,595547,595546,595543,595525,595620,595640,595527,595526,595641,605604,595647,595635,605605,605606,595666,595665,595667,595676,595675,595644,595657,595674,595656,595664,595655,595636,595646,595654,595645,605624,605623,605636,605626,605635,605625,605634,605633,605615,605614,605604,605613,605603,605607,605605,605606,605616,605617,605627,605641,605527,605642,605537,605624,605611,605610,605623,605634,605620,605602,605633,605632,605516,605631,605622,605613,605612,605621,605630,605603,605517,595634,595621,595622,595635,595623,595632,595633,595624,595612,595631,595613,595653,595652,595642,595641,595644,595643,595645,595654,605642,605643,605644,605645,605663,605664,605665,615626,605625,615605,615645,615604,615606,615613,615632,615614,615633,615634,615635,605654,605655,605652,605653,605674,605675,605636,605635,615616,605634,615615,605633,615603,615624,615625,615622,615623,605732,605731,605730,605721,605720,605723,605741,605740,605722,605742,605637,605607,605617,605627,605710,605711,605712,605700,605701,605667,605741,605645,605646,605740,605665,605647,605760,605666,605761,605762,615703,615605,615607,615606,605655,605676,605771,605677,605772,605752,605656,605657,605675,605751,605770,605637,605750,605636,615617,605635,615616,605773,605774,615702,615701,615700,605627,605641,605642,605537,605556,605557,605663,605660,605661,605662,615604,605640,615612,615613,605652,605653,605546,605674,605547,605672,605673,605670,605671,605650,605651,605631,605630,615602,615603,615601,605567,605566";
	private static String inPutFileFolder = "G:\\didi\\didi5h\\tmp";
	private static String outPutFileFloder = "G:\\didi\\minData1\\";

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		HashMap<String, ArrayList<String>> outMap = new HashMap<>();
		File[] files = new File(inPutFileFolder).listFiles();
		TreeMap<Integer, File> treeMap = new TreeMap<>();
		long num = 0;
		for (File inPutFile : files) {
			int name = Integer.parseInt(inPutFile.getName().replace(".dat", ""));
			File file = inPutFile;
			treeMap.put(name, file);
		}
		for (Map.Entry<Integer, File> entry : treeMap.entrySet()) {
			int name = entry.getKey();
			log.info("kaishi chuli: " + name);
			File inPutFile = entry.getValue();
			try {
				BufferedReader br = new BufferedReader(new FileReader(inPutFile));
				String line = "";
				String beforeTime = "";
				ArrayList<String> dataList = new ArrayList<>();
				while ((line = br.readLine()) != null) {
					DiDiOrignalBean didi = DiDiOrignalBean.parseData(line);
					Integer meshId = MapUtil.findMesh(didi.lon, didi.lat);
					if (BEIJING_MESHID.contains(meshId + "")) {
						num++;
						String yyyymmddHHmm = getDiditime(didi.timeStamp);
						ArrayList<String> lineList = outMap.get(yyyymmddHHmm);
						if (lineList == null) {
							ArrayList<String> lineList1 = new ArrayList<>();
							lineList1.add(line);
							outMap.put(yyyymmddHHmm, lineList1);
						} else {
							lineList.add(line);
						}
						if (num % 1000 == 0) {
							getOut(outMap);
							outMap.clear();
							num=0;
						}

					} else {
						continue;
					}
				}
				if (dataList.size() != 0) {
					// outToFile(dataList, beforeTime);
					// dataList.clear();
					getOut(outMap);
					outMap.clear();
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
			log.info("jieshu chuli: " + name);
		}
	}

	private static void getOut(HashMap<String, ArrayList<String>> outMap) throws IOException {
		// TODO Auto-generated method stub
		for (Map.Entry<String, ArrayList<String>> entry : outMap.entrySet()) {
			String yyyymmddHHmm = entry.getKey();
			String outFile = outPutFileFloder + yyyymmddHHmm;
			BufferedWriter write = new BufferedWriter(new FileWriter(outFile, true));
			for (String line : entry.getValue()) {
				write.write(line);
				write.newLine();
				write.flush();
			}
			write.close();
		}

	}

	private static void outToFile(ArrayList<String> dataList, String yyyymmddHHmm) throws IOException {
		// TODO Auto-generated method stub
		String outFile = outPutFileFloder + yyyymmddHHmm;
		BufferedWriter write = new BufferedWriter(new FileWriter(outFile, true));
		for (String line : dataList) {
			write.write(line);
			write.newLine();
			write.flush();
		}
		write.close();
	}

	private static String getDiditime(long t) {
		// TODO Auto-generated method stub
		t = t * 1000;
		java.util.Date d = new Date();
		d.setTime(t);
		String dateTime = hf.format(d);
		return dateTime;
	}

}
